#include <stdio.h>
#include <fcntl.h>
#include "video_adjust.h"
#include "i2c_drv.h"
#include "debug.h"

static int video_adjust_fd = -1;

int video_adjust_brightness(unsigned char video_cnt,char brightness){

    if(video_adjust_fd == -1){
        video_adjust_fd = open(NVP_I2C, O_RDWR);
        if(video_adjust_fd < 0)
        {
            pr_warn("Open i2c file error:%d\n",video_adjust_fd);
            return -1;
        }
    }
    if(video_cnt<4){
        i2c_write_bytes(video_adjust_fd,NVP1_ADDR,0x20+(video_cnt%4), &brightness, 1);
    }
    else{
        i2c_write_bytes(video_adjust_fd,NVP2_ADDR,0x20+(video_cnt%4), &brightness, 1);
    }
    return 0;
}
char video_read_brightness(unsigned char video_cnt){
    char brightness;
    if(video_adjust_fd == -1){
        video_adjust_fd = open(NVP_I2C, O_RDWR);
        if(video_adjust_fd < 0)
        {
            pr_warn("Open i2c file error:%d\n",video_adjust_fd);
            return -1;
        }
    }
    if(video_cnt<4){
        i2c_read_bytes(video_adjust_fd,NVP1_ADDR,0x20+(video_cnt%4), &brightness, 1);
    }
    else{
        i2c_read_bytes(video_adjust_fd,NVP2_ADDR,0x20+(video_cnt%4), &brightness, 1);
    }
    return brightness;
}

int video_adjust_contrast(unsigned char video_cnt,unsigned char constrast){
    if(video_adjust_fd == -1){
        video_adjust_fd = open(NVP_I2C, O_RDWR);
        if(video_adjust_fd < 0)
        {
            pr_warn("Open i2c file error:%d\n",video_adjust_fd);
            return -1;
        }
    }
    if(video_cnt<4){
        i2c_write_bytes(video_adjust_fd,NVP1_ADDR,0x24+(video_cnt%4), &constrast, 1);
    }
    else{
        i2c_write_bytes(video_adjust_fd,NVP2_ADDR,0x24+(video_cnt%4), &constrast, 1);
    }
    return 0;
}
unsigned char video_read_contrast(unsigned char video_cnt){
    unsigned char constrast;
    if(video_adjust_fd == -1){
        video_adjust_fd = open(NVP_I2C, O_RDWR);
        if(video_adjust_fd < 0)
        {
            pr_warn("Open i2c file error:%d\n",video_adjust_fd);
            return -1;
        }
    }
    if(video_cnt<4){
        i2c_read_bytes(video_adjust_fd,NVP1_ADDR,0x24+(video_cnt%4), &constrast, 1);
    }
    else{
        i2c_read_bytes(video_adjust_fd,NVP2_ADDR,0x24+(video_cnt%4), &constrast, 1);
    }
    return constrast;
}

int video_adjust_saturation(unsigned char video_cnt,unsigned char saturation){
    if(video_adjust_fd == -1){
        video_adjust_fd = open(NVP_I2C, O_RDWR);
        if(video_adjust_fd < 0)
        {
            pr_warn("Open i2c file error:%d\n",video_adjust_fd);
            return -1;
        }
    }
    if(video_cnt<4){
        i2c_write_bytes(video_adjust_fd,NVP1_ADDR,0x24+(video_cnt%4), &saturation, 1);
    }
    else{
        i2c_write_bytes(video_adjust_fd,NVP2_ADDR,0x24+(video_cnt%4), &saturation, 1);
    }
    return 0;
}
unsigned char video_read_saturation(unsigned char video_cnt){
    unsigned char saturation;
    if(video_adjust_fd == -1){
        video_adjust_fd = open(NVP_I2C, O_RDWR);
        if(video_adjust_fd < 0)
        {
            pr_warn("Open i2c file error:%d\n",video_adjust_fd);
            return -1;
        }
    }
    if(video_cnt<4){
        i2c_read_bytes(video_adjust_fd,NVP1_ADDR,0x58+(video_cnt%4), &saturation, 1);
    }
    else{
        i2c_read_bytes(video_adjust_fd,NVP2_ADDR,0x58+(video_cnt%4), &saturation, 1);
    }
    return saturation;
}

